/**
 * 学生获取自己的实习报名情况
 * 
 * /student/jobdetail
 */

 

const utils = require('../../../utils/index')
const $ = require('../../../utils/common')
const User_Info = require('../../../models/usersInfo')
const InternshipStudents = require('../../../models/internshipStudents')
const Companies = require('../../../models/companys')

const get_details = async function(req, res) {
    try {
        if (!req.body.year_id) {
            $.sendMessage(res, 1015, ' 参数year_id 错误')
            return
        }

        const student = await InternshipStudents.findOne({
            where: {
                student_user_name: req.user_name,
                student_year: req.body.year_id
            }
        })

        if (student == null) {
            $.sendMessage(res, 1015, ' 靓仔你不属于该年度 ')
            return
        }
       
        // 需要联合查询
        // 用户详情
        await InternshipStudents.belongsTo(User_Info,
            {
                foreignKey: 'student_user_name',
                targetKey: 'user_name'
            })  

        const student_info = await InternshipStudents.findOne({
            where: {
                student_user_name: req.user_name
            },
            attributes: ['student_status', 'teacher_user_fullname', 'company_name',
                    'company_id', 'is_locked', ],
            include: [
                {
                    model: User_Info,
                    attributes: ['user_fullname', 'user_sex', 'user_major',
                    'user_phone', 'user_email', 'user_name'],
                }
            ],
            raw: true
        })
        
        var student_job = {}  // 学生实习信息对象
        // 如果学生有报名则返回报名相关的公司的信息
        // 如果没有报名则返回空student_job对象
        if (student.dataValues.student_status == '1') {
            const company_info = await Companies.findOne({
                where: {
                    id: student.dataValues.company_id
                },
                attributes: ['company_website', 'company_phone', 'company_email',
                    'company_contacts', 'company_address', 'job_name',
                    'job_duty', 'requisite_skills', 'salary',
                     'working_time', 'type']
            })
            student_job = company_info.dataValues
            student_info['student_job'] = student_job
            res.json({
                code: 200,
                message: '获取成功',
                data: student_info
            })
            return
        }
        else if (student.dataValues.student_status == '0') {
            res.json({
                code: 200,
                message: '获取成功',
                data: student_info
            })
            return
        }
        else if (student.dataValues.student_status != '1' && student.dataValues.student_status != '0'){
            $.sendMessage(res, 0000, ' 数据库数据被污染，请联系管理员')
            return
        }
      
        utils.create_a_log(req, '学生入所', '浏览详情')
    }

    catch(err) {
        $.sendMessage(res, 0000, "err:" + err)
        utils.create_a_log(req, '学生入所', '浏览详情', '失败')
        return
    }
}

module.exports = get_details